//
//  OrderDetailController.m
//  jang
//
//  Created by lee han on 29/05/2017.
//  Copyright © 2017 lee han. All rights reserved.
//

#import "OrderDetailController.h"
#import "OrderCell.h"
#import "CommonView.h"
#import "PayViewController.h"
#import "MainController.h"
#import "CRUserData.h"

@interface OrderDetailController ()

@end

@implementation OrderDetailController

- (void)requestProductInfo:(NSString*) orderid {
    NSMutableDictionary* data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                 [_mOrderInfo objectForKey:@"id"],  @"orderid",
                                 [_mOrderInfo objectForKey:@"addressid"],  @"addressid",
                                 nil];
    
    [[network sharedInstance] startHttpRequest:self Data:data functionType:NetworkFunctionType_OrderDetail];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationItem.backBarButtonItem setTitle:@""];
    
    _mProductInfo = [[NSMutableArray alloc] init];
    
    _mTableView.delegate = self;
    _mTableView.dataSource = self;
    
    [_mTableView registerNib:[UINib nibWithNibName:@"OrderCell" bundle:nil] forCellReuseIdentifier:@"OrderCell"];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    if(_mOrderInfo != NULL)
    {
        [self requestProductInfo:[_mOrderInfo objectForKey:@"id"]];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)onClickButton:(id)sender {
    if(_mType == 1) {
        [self performSegueWithIdentifier:@"list2pay" sender:self];
    }
    else if (_mType == 2) {
        //发送确认收货信息
        NSMutableDictionary* data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                     [_mOrderInfo objectForKey:@"id"],  @"orderid",
                                     nil];
        
        [[network sharedInstance] startHttpRequest:self Data:data functionType:NetworkFunctionType_OrderReceive];
    }
    else if(_mType == 4) {
        //发送再次购买信息
        NSMutableDictionary* data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                     [_mOrderInfo objectForKey:@"id"],  @"orderid",
                                     nil];
        
        [[network sharedInstance] startHttpRequest:self Data:data functionType:NetworkFunctionType_OrderRebuy];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark UITableViewDelegate

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row < 2 || indexPath.row >= [_mProductInfo count] + 2)
        return 31;
    return 70;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

//end UITableViewDelegate

#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger ret = 0;
    if([_mProductInfo count] > 0) {
        ret += [_mProductInfo count] + 7;
        if (_mType != 3) {
            ret += 1;
        }
    }
    
    return ret;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.row >= 2 && indexPath.row < [_mProductInfo count] + 2){
        OrderCell *cell = [_mTableView dequeueReusableCellWithIdentifier:@"OrderCell" forIndexPath:indexPath];
        
        [cell initWithData:[_mProductInfo objectAtIndex:indexPath.row - 2]];
        
        return cell;
    }
    
    UITableViewCell* tcell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Ordercell1"];
    CGRect tfram = tcell.frame;
    tfram.size.height = 30;
    tcell.frame = tfram;
    tcell.backgroundColor = [UIColor whiteColor];
    
    [tcell setSelectionStyle:UITableViewCellSelectionStyleNone];
    tcell.textLabel.font = [UIFont systemFontOfSize:13];
    
    NSInteger downStartIndex = [_mProductInfo count] + 2;
    
    if (indexPath.row == 0) {
        tcell.textLabel.text = @"订单号";
        tcell.detailTextLabel.font = [UIFont systemFontOfSize:13];
        tcell.detailTextLabel.text = [_mOrderInfo objectForKey:@"code"];
    }
    else if (indexPath.row == 1) {
        tcell.textLabel.text = @"创建时间";
        tcell.detailTextLabel.font = [UIFont systemFontOfSize:13];
        tcell.detailTextLabel.text = [_mOrderInfo objectForKey:@"createtime"];
    }
    else if(indexPath.row == downStartIndex){
        tcell.textLabel.text = @"姓名";
        tcell.detailTextLabel.font = [UIFont systemFontOfSize:13];
        tcell.detailTextLabel.text = [_mAddressInfo objectForKey:@"name"];
    }
    else if (indexPath.row == downStartIndex + 1){
        tcell.textLabel.text = @"电话";
        tcell.detailTextLabel.font = [UIFont systemFontOfSize:13];
        tcell.detailTextLabel.text = [_mAddressInfo objectForKey:@"phone"];
    }
    else if (indexPath.row == downStartIndex + 2){
        tcell.textLabel.text = @"地址";
        tcell.detailTextLabel.font = [UIFont systemFontOfSize:13];
        tcell.detailTextLabel.text = [_mAddressInfo objectForKey:@"address"];
    }
    else if (indexPath.row == downStartIndex + 3){
        tcell.textLabel.text = @"运费";
        tcell.detailTextLabel.font = [UIFont systemFontOfSize:13];
        tcell.detailTextLabel.textColor = [UIColor redColor];
        tcell.detailTextLabel.text = @"以实际配送为准";
    }
    else if (indexPath.row == downStartIndex + 4){
        tcell.textLabel.textColor = [UIColor redColor];
        tcell.textLabel.text = @"金额";
        
        tcell.detailTextLabel.font = [UIFont systemFontOfSize:12];
        tcell.detailTextLabel.textColor = [UIColor redColor];
        tcell.detailTextLabel.text = [_mOrderInfo objectForKey:@"money"];
    }
    else if (indexPath.row == downStartIndex + 5) {
        tcell.textLabel.text = @"";
        tcell.detailTextLabel.text = @"";
        
        UIButton* button = [[UIButton alloc] initWithFrame:tfram];
        button.backgroundColor = [CommonView NavigationColor];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont systemFontOfSize:14]];
        
        if (_mType == 1) {
            [button setTitle:@"立即支付" forState:UIControlStateNormal];
        }else if (_mType == 2){
            [button setTitle:@"确认收货" forState:UIControlStateNormal];
        }else if (_mType == 4) {
            [button setTitle:@"再次购买" forState:UIControlStateNormal];
        }
        [button addTarget:self action:@selector(onClickButton:) forControlEvents:UIControlEventTouchUpInside];
        [tcell addSubview:button];
    }
    
    if(indexPath.row != downStartIndex + 5) {
        UIView* lineview = [[UIView alloc] initWithFrame:CGRectMake(0, 30, tfram.size.width, 1)];
        lineview.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [tcell addSubview:lineview];
    }
    
    return tcell;
}


//end UITableViewDataSource

#pragma mark networkDelegate
- (void)parserResult:(NSDictionary *)response functionType:(NSInteger)type
{
    if (type == NetworkFunctionType_OrderDetail){
        if([[response objectForKey:@"status"] integerValue] == 1){
            [_mProductInfo removeAllObjects];
            
            if ([response objectForKey:@"data"] != NULL && ![[response objectForKey:@"data"] isEqual:[NSNull null]]) {
                for (NSDictionary* data in [response objectForKey:@"data"]) {
                    [_mProductInfo addObject:data];
                }
            }
            
            _mAddressInfo = [response objectForKey:@"address"];
            
            [_mTableView reloadData];
        }
        else{
            [CommonView addAlertView:self Text:@"订单产品列表错误"];
        }
    }
    else if (type == NetworkFunctionType_OrderReceive) {
        if([[response objectForKey:@"status"] integerValue] == 1) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        else {
            [CommonView addAlertView:self Text:@"确认收货失败"];
        }
        
    }
    else if (type == NetworkFunctionType_OrderRebuy) {
        if([[response objectForKey:@"status"] integerValue] == 1) {
            //跳转到购物车界面？
            [[CRUserData sharedInstance] setShopCarData:[response objectForKey:@"shopcar"]];
            [[MainController sharedInstance] selectShopcarView];
        }
        else {
            [CommonView addAlertView:self Text:@"再次购买失败"];
        }
    }
}
//end networkDelegate


- (void)prepareForSegue: (UIStoryboardSegue *)segue sender:(id)sender{
    PayViewController *destination = segue.destinationViewController;
    
    destination.mOrderData = _mOrderInfo;
    destination.mType = 1;
}
@end

